/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import entities.Supplier;
import entities.SupplierXpyme;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Andrés Felipe
 */
@Stateless
@Remote(SupplierXpymeFacadeRemote.class)
public class SupplierXpymeFacade extends AbstractFacade<SupplierXpyme> implements SupplierXpymeFacadeRemote {
    @PersistenceContext(unitName = "Javeriana.EJB.EstimatorPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public SupplierXpymeFacade() {
        super(SupplierXpyme.class);
    }

    @Override
    public void create(int pymeIdentification, int supplierIdentification) {
        SupplierXpyme supplierXpyme = new SupplierXpyme(supplierIdentification, pymeIdentification);
        this.create(supplierXpyme);
    }

    @Override
    public List<List<String>> getSuppliersInformation(int pymeIdentification) {
        Query query = em.createNamedQuery("SupplierXpyme.findByPyme");
        query.setParameter("identification", pymeIdentification);
        List<Supplier> suppliers = (List<Supplier>)query.getResultList();
        
        List<List<String>> result = new ArrayList<>();
        for(int i = 0; i < suppliers.size(); i++) {
            List<String> row = new ArrayList<>();
            row.add(String.valueOf(suppliers.get(i).getIdentification()));
            row.add(suppliers.get(i).getName());
            row.add(suppliers.get(i).getAddress());
            row.add(suppliers.get(i).getEmail());
            row.add(suppliers.get(i).getPhone());
            result.add(row);
        }
        
        return result;
    }
}
